﻿#if NETFX || NETCORE

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Apewer.Surface
{

    /// <summary>基本控件。</summary>
    public class BaseControl : UserControl
    {

        #region this

        /// <summary>组件。</summary>
        protected IContainer _components = null;

        /// <summary>释放资源。</summary>
        public new void Dispose()
        {
            if (!_disposed)
            {
                base.Dispose();
                _disposed = true;
            }
        }

        /// <summary>释放资源。</summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (_components != null)) _components.Dispose();
            base.Dispose(disposing);
        }

        /// <summary>初始化组件。</summary>
        protected void InitializeComponent()
        {
            _components = new Container();
            this.AutoScaleMode = AutoScaleMode.None;
            this.Font = DefaultFont;
            this.Size = new Size(300, 300);
            this.VerticalScroll.SmallChange = 20;
            this.VerticalScroll.LargeChange = 40;
            //this.BackColor = System.Drawing.Color.White;
            //this.MouseWheel += event_caption_mousewheel;
            //this.MouseMove += event_caption_mousemove;
        }

        /// <summary>构造函数。</summary>
        public BaseControl()
        {
            Optimization = true;

            this.VerticalScroll.SmallChange = 10;
            this.VerticalScroll.LargeChange = 20;
            this.HorizontalScroll.SmallChange = 10;
            this.HorizontalScroll.LargeChange = 20;

            this.MouseWheel += Event_Caption_MouseWheel;
            this.MouseMove += Event_Caption_MouseMove;
            this.HandleCreated += Event_HandleCreated;
            this.HandleDestroyed += Event_HandleDestroyed;
        }

        #endregion

        #region variable

        private int _index = 0;
        private string _key = "";
        private bool _locked = false;
        private bool _basecreated = false;
        private bool _disposed = false;

        #endregion

        #region accessor

        /// <summary>界面线程调用器。</summary>
        protected delegate void Invoker();

        /// <summary>已改变锁定属性。</summary>
        public event EventHandler LockedChanged;

        /// <summary>索引。</summary>
        public virtual int Index
        {
            get { return _index; }
            set { _index = value; }
        }

        /// <summary>标识。</summary>
        public virtual string Key
        {
            get { return _key; }
            set { _key = string.IsNullOrEmpty(value) ? "" : value; }
        }

        /// <summary>锁定。</summary>
        public virtual bool Locked
        {
            get { return _locked; }
            set
            {
                bool vold = _locked;
                _locked = value;
                if ((vold != value) && (LockedChanged != null)) LockedChanged(this, new EventArgs());
            }
        }

        /// <summary>基对象已创建。</summary>
        public bool BaseCreated
        {
            get { return _basecreated; }
            private set { _basecreated = value; }
        }

        /// <summary>鼠标滚轮事件。</summary>
        public void WheelMouse(MouseEventArgs e)
        {
            OnMouseWheel(e);
        }

        /// <summary>启用优化。</summary>
        protected virtual bool Optimization
        {
            set
            {
                SetStyle(ControlStyles.UserPaint, value);
                SetStyle(ControlStyles.AllPaintingInWmPaint, value);
                SetStyle(ControlStyles.OptimizedDoubleBuffer, value);
                // SetStyle(ControlStyles.ResizeRedraw, value);
                SetStyle(ControlStyles.DoubleBuffer, value);
                UpdateStyles();
            }
        }

        #endregion

        #region event

        private void Event_Caption_MouseMove(object sender, MouseEventArgs e)
        {
            // if (AutoScroll) Focus();
        }

        private void Event_Caption_MouseWheel(object sender, MouseEventArgs e)
        {
            if (AutoScroll)
            {
                //Refresh();
                //Invalidate();
                //Update();
            }
        }

        private void Event_HandleDestroyed(object sender, EventArgs e)
        {
            BaseCreated = false;
        }

        private void Event_HandleCreated(object sender, EventArgs e)
        {
            BaseCreated = true;
        }

        #endregion

    }

}

#endif
